package com.bite.system.controller.sysuser;

import com.bite.common.core.constants.HttpConstants;
import com.bite.common.core.controller.BaseController;
import com.bite.common.core.domain.R;
import com.bite.common.core.domain.vo.LoginUserVO;
import com.bite.system.domain.system.dto.LoginDTO;
import com.bite.system.domain.system.dto.SysUserAddDTO;
import com.bite.system.domain.system.dto.SysUserUpdateDTO;
import com.bite.system.domain.system.vo.SysUserVO;
import com.bite.system.service.sysuser.ISysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

// system/sysUser/..
@RequestMapping("/sysUser")
@RestController
@Tag(name = "管理员接口", description = "存放管理员API相关信息与详情")
public class SysUserController extends BaseController {

    @Autowired
    private ISysUserService sysUserService;

    // 路径 /system/sysUser/login
    @Operation(summary = "管理员登录", description = "提供用户名和密码")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "1000", description = "登录成功")
    @PostMapping("/login")
    public R<String> login(@RequestBody LoginDTO loginDTO) {

        return ok(sysUserService.login(loginDTO.getUserAccount(), loginDTO.getPassword()));
    }

    // 登出
    // 路径 /system/sysUser/logout
    @DeleteMapping("/logout")
    public R<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
//        return sysUserService.logout(token) ? R.ok() : R.fail();
        token = transToken(token);
        return toR(sysUserService.logout(token)); // 通过BaseController 中的方法
    }

    // 路径 /system/sysUser/info
    // 获取登录信息
    @GetMapping("/info")
    public R<LoginUserVO> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        token = transToken(token);
        return ok(sysUserService.info(token));
    }




    @Operation(summary = "管理员添加", description = "根据提供的信息新增管理员⽤⼾")
    @ApiResponse(responseCode = "3101", description = "管理员已存在")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "1000", description = "添加成功")
    @PostMapping("/add")
    public R<Void> add(@RequestBody SysUserAddDTO sysUserSaveDTO) {
        return toR(sysUserService.add(sysUserSaveDTO));
    }

    @PutMapping("/update")
    public R<Void> update(@RequestBody SysUserUpdateDTO userUpdateDTO) {
        return toR(sysUserService.update(userUpdateDTO));
    }


    @DeleteMapping("/{userId}")
    @Operation(summary = "删除⽤⼾", description = "通过⽤⼾id删除⽤⼾")
    @Parameters(value = {
            @Parameter(name = "userId", in = ParameterIn.PATH, description = "⽤⼾ID")
    })
    @ApiResponse(responseCode = "1000", description = "成功删除⽤⼾")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3101", description = "⽤⼾不存在")
    public R<Void> delete(@PathVariable Long userId) {
        return null;
    }

    @GetMapping("/detail")
    @Parameters(value = {
            @Parameter(name = "userId", in = ParameterIn.QUERY, description = "⽤⼾ID"),
    })
    @ApiResponse(responseCode = "1000", description = "成功获取⽤⼾信息")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3101", description = "⽤⼾不存在")
    public R<SysUserVO> detail(Long userId) {
        return ok(sysUserService.detail(userId));
    }

}
